home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / hplip / fab.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-28  |  19.0 KB  |  674 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __version__ = '6.0'
  5. __title__ = 'Fax Address Book'
  6. __mod__ = 'hp-fab'
  7. __doc__ = 'A simple fax address book for HPLIP.'
  8. import cmd
  9. import getopt
  10. import os
  11. from base.g import *
  12. from base import utils, tui, module
  13.  
  14. class Console(cmd.Cmd):
  15.     
  16.     def __init__(self):
  17.         cmd.Cmd.__init__(self)
  18.         self.intro = "Type 'help' for a list of commands. Type 'exit' or 'quit' to quit."
  19.         self.db = fax.FaxAddressBook()
  20.         self.prompt = log.bold('hp-fab > ')
  21.  
  22.     
  23.     def do_hist(self, args):
  24.         '''Print a list of commands that have been entered'''
  25.         print self._hist
  26.  
  27.     
  28.     def do_exit(self, args):
  29.         '''Exits from the console'''
  30.         return -1
  31.  
  32.     
  33.     def do_quit(self, args):
  34.         '''Exits from the console'''
  35.         return -1
  36.  
  37.     
  38.     def do_EOF(self, args):
  39.         '''Exit on system end of file character'''
  40.         return self.do_exit(args)
  41.  
  42.     
  43.     def do_help(self, args):
  44.         """Get help on commands
  45.            'help' or '?' with no arguments prints a list of commands for which help is available
  46.            'help <command>' or '? <command>' gives help on <command>
  47.         """
  48.         cmd.Cmd.do_help(self, args)
  49.  
  50.     
  51.     def preloop(self):
  52.         '''Initialization before prompting user for commands.
  53.            Despite the claims in the Cmd documentaion, Cmd.preloop() is not a stub.
  54.         '''
  55.         cmd.Cmd.preloop(self)
  56.         self._hist = []
  57.         self._locals = { }
  58.         self._globals = { }
  59.         self.do_list('')
  60.  
  61.     
  62.     def postloop(self):
  63.         '''Take care of any unfinished business.
  64.            Despite the claims in the Cmd documentaion, Cmd.postloop() is not a stub.
  65.         '''
  66.         cmd.Cmd.postloop(self)
  67.         print 'Exiting...'
  68.  
  69.     
  70.     def precmd(self, line):
  71.         ''' This method is called after the line has been input but before
  72.             it has been interpreted. If you want to modifdy the input line
  73.             before execution (for example, variable substitution) do it here.
  74.         '''
  75.         self._hist += [
  76.             line.strip()]
  77.         return line
  78.  
  79.     
  80.     def postcmd(self, stop, line):
  81.         '''If you want to stop the console, return something that evaluates to true.
  82.            If you want to do some post command processing, do it here.
  83.         '''
  84.         return stop
  85.  
  86.     
  87.     def emptyline(self):
  88.         '''Do nothing on empty input line'''
  89.         pass
  90.  
  91.     
  92.     def default(self, line):
  93.         log.error("Unrecognized command. Use 'help' to list commands.")
  94.  
  95.     
  96.     def get_nickname(self, args, fail_if_match = True, alt_text = False):
  97.         if not args:
  98.             while True:
  99.                 if alt_text:
  100.                     nickname = raw_input(log.bold('Enter the name to add to the group (<enter>=done*, c=cancel) ? ')).strip()
  101.                 else:
  102.                     nickname = raw_input(log.bold('Enter name (c=cancel) ? ')).strip()
  103.                 if nickname.lower() == 'c':
  104.                     print log.red('Canceled')
  105.                     return ''
  106.                 if fail_if_match:
  107.                     if self.db.get(nickname) is not None:
  108.                         log.error('Name already exists. Please choose a different name.')
  109.                         continue
  110.                     
  111.                 elif self.db.get(nickname) is None:
  112.                     log.error('Name not found. Please enter a different name.')
  113.                     continue
  114.                 
  115.                 break
  116.                 continue
  117.                 None if not nickname else nickname.lower() == 'c'
  118.         else:
  119.             nickname = args.strip()
  120.             if fail_if_match:
  121.                 if self.db.get(nickname) is not None:
  122.                     log.error('Name already exists. Please choose a different name.')
  123.                     return ''
  124.             elif self.db.get(nickname) is None:
  125.                 log.error('Name not found. Please enter a different name.')
  126.                 return ''
  127.         return nickname
  128.  
  129.     
  130.     def get_groupname(self, args, fail_if_match = True, alt_text = False):
  131.         all_groups = self.db.get_all_groups()
  132.         if not args:
  133.             while True:
  134.                 if alt_text:
  135.                     groupname = raw_input(log.bold('Enter the group to join (<enter>=done*, c=cancel) ? ')).strip()
  136.                 else:
  137.                     groupname = raw_input(log.bold('Enter the group (c=cancel) ? ')).strip()
  138.                 if groupname.lower() == 'c':
  139.                     print log.red('Canceled')
  140.                     return ''
  141.                 if groupname == 'All':
  142.                     print "Cannot specify group 'All'. Please choose a different group."
  143.                     return ''
  144.                 if fail_if_match:
  145.                     if groupname in all_groups:
  146.                         log.error('Group already exists. Please choose a different group.')
  147.                         continue
  148.                     
  149.                 elif groupname not in all_groups:
  150.                     log.error('Group not found. Please enter a different group.')
  151.                     continue
  152.                 
  153.                 break
  154.         else:
  155.             groupname = args.strip()
  156.             if fail_if_match:
  157.                 if groupname in all_groups:
  158.                     log.error('Group already exists. Please choose a different group.')
  159.                     return ''
  160.             elif groupname not in all_groups:
  161.                 log.error('Group not found. Please enter a different group.')
  162.                 return ''
  163.         return groupname
  164.  
  165.     
  166.     def do_list(self, args):
  167.         '''
  168.         List names and/or groups.
  169.         list [names|groups|all|]
  170.         dir [names|groups|all|]
  171.         '''
  172.         self.do_names('')
  173.         self.do_groups('')
  174.  
  175.     do_dir = do_list
  176.     
  177.     def do_names(self, args):
  178.         '''
  179.         List names.
  180.         names
  181.         '''
  182.         all_entries = self.db.get_all_records()
  183.         log.debug(all_entries)
  184.         print log.bold('\nNames:\n')
  185.         if len(all_entries) > 0:
  186.             f = tui.Formatter()
  187.             f.header = ('Name', 'Fax Number', 'Notes', 'Member of Group(s)')
  188.             for name, e in all_entries.items():
  189.                 if not name.startswith('__'):
  190.                     f.add((name, e['fax'], e['notes'], ', '.join(e['groups'])))
  191.                     continue
  192.             
  193.             f.output()
  194.         else:
  195.             print '(None)'
  196.         print 
  197.  
  198.     
  199.     def do_groups(self, args):
  200.         '''
  201.         List groups.
  202.         groups
  203.         '''
  204.         all_groups = self.db.get_all_groups()
  205.         log.debug(all_groups)
  206.         print log.bold('\nGroups:\n')
  207.         if len(all_groups):
  208.             f = tui.Formatter()
  209.             f.header = ('Group', 'Members')
  210.             for group in all_groups:
  211.                 ', '.join(([], [](_[1])))
  212.             
  213.             f.output()
  214.         else:
  215.             print '(None)'
  216.         print 
  217.  
  218.     
  219.     def do_edit(self, args):
  220.         '''
  221.         Edit an name.
  222.         edit [name]
  223.         modify [name]
  224.         '''
  225.         nickname = self.get_nickname(args, fail_if_match = False)
  226.         if not nickname:
  227.             return None
  228.         e = self.db.get(nickname)
  229.         log.debug(e)
  230.         print log.bold('\nEdit/modify information for %s:\n' % nickname)
  231.         lastname = ''
  232.         firstname = ''
  233.         title = ''
  234.         save_faxnum = e['fax']
  235.         while True:
  236.             faxnum = raw_input(log.bold("Fax Number (<enter>='%s', c=cancel) ? " % save_faxnum)).strip()
  237.             if faxnum.lower() == 'c':
  238.                 print log.red('Canceled')
  239.                 return None
  240.             if not faxnum and not save_faxnum:
  241.                 log.error('Fax number must not be empty.')
  242.                 continue
  243.             
  244.             if not faxnum:
  245.                 faxnum = save_faxnum
  246.             
  247.             ok = True
  248.             for c in faxnum:
  249.                 if c not in '0123456789-(+) *#':
  250.                     log.error("Invalid characters in fax number. Fax number may only contain '0123456789-(+) '")
  251.                     ok = False
  252.                     break
  253.                     continue
  254.             
  255.             if ok:
  256.                 break
  257.                 continue
  258.         save_notes = e['notes']
  259.         notes = raw_input(log.bold("Notes (<enter>='%s', c=cancel) ? " % save_notes)).strip()
  260.         if notes.lower() == 'c':
  261.             print log.red('Canceled')
  262.             return None
  263.         if not notes:
  264.             notes = save_notes
  265.         
  266.         if e['groups']:
  267.             print '\nLeave or Stay in a Group:\n'
  268.         
  269.         new_groups = []
  270.         for g in e['groups']:
  271.             if g == 'All':
  272.                 continue
  273.             
  274.             (ok, ans) = tui.enter_yes_no('Stay in group %s ' % g, choice_prompt = '(y=yes* (stay), n=no (leave), c=cancel) ? ')
  275.             if not ok:
  276.                 print log.red('Canceled')
  277.                 return None
  278.             if ans:
  279.                 new_groups.append(g)
  280.                 continue
  281.             ok
  282.         
  283.         print '\nJoin New Group(s):\n'
  284.         while True:
  285.             add_group = self.get_groupname('', fail_if_match = False, alt_text = True)
  286.             if add_group.lower() == 'c':
  287.                 print log.red('Canceled')
  288.                 return None
  289.             if not add_group:
  290.                 break
  291.             
  292.             all_groups = self.db.get_all_groups()
  293.             if add_group not in all_groups:
  294.                 log.warn('Group not found.')
  295.                 (ok, ans) = tui.enter_yes_no('Is this a new group', choice_prompt = '(y=yes* (new), n=no, c=cancel) ? ')
  296.                 if not ok:
  297.                     print log.red('Canceled')
  298.                     return None
  299.                 if not ans:
  300.                     continue
  301.                 
  302.             
  303.             if add_group in e['groups']:
  304.                 log.error('Group already specified. Choose a different group name or press <enter> to continue.')
  305.                 continue
  306.             
  307.             new_groups.append(add_group)
  308.         self.db.set(nickname, title, firstname, lastname, faxnum, new_groups, notes)
  309.         self.do_show(nickname)
  310.         print 
  311.  
  312.     do_modify = do_edit
  313.     
  314.     def do_editgrp(self, args):
  315.         '''
  316.         Edit a group.
  317.         editgrp [group]
  318.         modifygrp [group]
  319.         '''
  320.         group = self.get_groupname(args, fail_if_match = False)
  321.         if not group:
  322.             return None
  323.         old_entries = self.db.group_members(group)
  324.         new_entries = []
  325.         print '\nExisting Names in Group:\n'
  326.         for e in old_entries:
  327.             if not e.startswith('__'):
  328.                 (ok, ans) = tui.enter_yes_no("Should '%s' stay in this group " % e, choice_prompt = '(y=yes* (stay), n=no (leave), c=cancel) ? ')
  329.             
  330.             if not ok:
  331.                 print log.red('Canceled')
  332.                 return None
  333.             if ans:
  334.                 new_entries.append(e)
  335.                 continue
  336.             ok
  337.         
  338.         print '\nAdd New Names to Group:\n'
  339.         while True:
  340.             nickname = self.get_nickname('', fail_if_match = False, alt_text = True)
  341.             if nickname.lower() == 'c':
  342.                 print log.red('Canceled')
  343.                 return None
  344.             if not nickname.lower():
  345.                 break
  346.             
  347.             new_entries.append(nickname)
  348.         self.db.update_groups(group, new_entries)
  349.         print 
  350.  
  351.     do_modifygrp = do_editgrp
  352.     
  353.     def do_add(self, args):
  354.         '''
  355.         Add an name.
  356.         add [name]
  357.         new [name]
  358.         '''
  359.         nickname = self.get_nickname(args, fail_if_match = True)
  360.         if not nickname:
  361.             return None
  362.         print log.bold('\nEnter information for %s:\n' % nickname)
  363.         title = ''
  364.         firstname = ''
  365.         lastname = ''
  366.         while True:
  367.             faxnum = raw_input(log.bold('Fax Number (c=cancel) ? ')).strip()
  368.             if faxnum.lower() == 'c':
  369.                 print log.red('Canceled')
  370.                 return None
  371.             if not faxnum:
  372.                 log.error('Fax number must not be empty.')
  373.                 continue
  374.             
  375.             ok = True
  376.             for c in faxnum:
  377.                 if c not in '0123456789-(+) *#':
  378.                     log.error("Invalid characters in fax number. Fax number may only contain '0123456789-(+) *#'")
  379.                     ok = False
  380.                     break
  381.                     continue
  382.             
  383.             if ok:
  384.                 break
  385.                 continue
  386.         notes = raw_input(log.bold('Notes (c=cancel) ? ')).strip()
  387.         if notes.strip().lower() == 'c':
  388.             print log.red('Canceled')
  389.             return None
  390.         groups = []
  391.         all_groups = self.db.get_all_groups()
  392.         while True:
  393.             add_group = raw_input(log.bold('Member of group (<enter>=done*, c=cancel) ? ')).strip()
  394.             if add_group.lower() == 'c':
  395.                 print log.red('Canceled')
  396.                 return None
  397.             if add_group not in all_groups:
  398.                 log.warn('Group not found.')
  399.                 while True:
  400.                     user_input = raw_input(log.bold('Is this a new group (y=yes*, n=no) ? ')).lower().strip()
  401.                     if user_input not in ('', 'n', 'y'):
  402.                         log.error("Please enter 'y', 'n' or press <enter> for 'yes'.")
  403.                         continue
  404.                     
  405.                     break
  406.                 if user_input == 'n':
  407.                     continue
  408.                 
  409.             
  410.             if add_group in groups:
  411.                 log.error('Group already specified. Choose a different group name or press <enter> to continue.')
  412.                 continue
  413.             
  414.             groups.append(add_group)
  415.         groups.append('All')
  416.         self.db.set(nickname, title, firstname, lastname, faxnum, groups, notes)
  417.         self.do_show(nickname)
  418.  
  419.     do_new = do_add
  420.     
  421.     def do_addgrp(self, args):
  422.         '''
  423.         Add a group.
  424.         addgrp [group]
  425.         newgrp [group]
  426.         '''
  427.         group = self.get_groupname(args, fail_if_match = True)
  428.         if not group:
  429.             return None
  430.         entries = []
  431.         while True:
  432.             nickname = self.get_nickname('', fail_if_match = False, alt_text = True)
  433.             if nickname.lower() == 'c':
  434.                 print log.red('Canceled')
  435.                 return None
  436.             entries.append(nickname)
  437.             continue
  438.             None if not nickname.lower() else group
  439.         self.db.update_groups(group, entries)
  440.         print 
  441.  
  442.     do_newgrp = do_addgrp
  443.     
  444.     def do_view(self, args):
  445.         '''
  446.         View all name data.
  447.         view
  448.         '''
  449.         all_entries = self.db.get_all_records()
  450.         log.debug(all_entries)
  451.         print log.bold('\nView all Data:\n')
  452.         if len(all_entries) > 0:
  453.             f = tui.Formatter()
  454.             f.header = ('Name', 'Fax', 'Notes', 'Member of Group(s)')
  455.             for name, e in all_entries.items():
  456.                 if not name.startswith('__'):
  457.                     f.add((name, e['fax'], e['notes'], ', '.join(e['groups'])))
  458.                     continue
  459.             
  460.             f.output()
  461.         
  462.         print 
  463.  
  464.     
  465.     def do_show(self, args):
  466.         '''
  467.         Show a name (all details).
  468.         show [name]
  469.         details [name]
  470.         '''
  471.         name = self.get_nickname(args, fail_if_match = False)
  472.         if not name:
  473.             return None
  474.         e = self.db.get(name)
  475.         if e:
  476.             f = tui.Formatter()
  477.             f.header = ('Key', 'Value')
  478.             f.add(('Name:', name))
  479.             f.add(('Fax Number:', e['fax']))
  480.             f.add(('Notes:', e['notes']))
  481.             f.add(('Member of Group(s):', ', '.join(e['groups'])))
  482.             f.output()
  483.         else:
  484.             log.error("Name not found. Use the 'names' command to view all names.")
  485.         print 
  486.  
  487.     do_details = do_show
  488.     
  489.     def do_rm(self, args):
  490.         '''
  491.         Remove a name.
  492.         rm [name]
  493.         del [name]
  494.         '''
  495.         nickname = self.get_nickname(args, fail_if_match = False)
  496.         if not nickname:
  497.             return None
  498.         self.db.delete(nickname)
  499.         print 
  500.  
  501.     do_del = do_rm
  502.     
  503.     def do_rmgrp(self, args):
  504.         '''
  505.         Remove a group.
  506.         rmgrp [group]
  507.         delgrp [group]
  508.         '''
  509.         group = self.get_groupname(args, fail_if_match = False)
  510.         if not group:
  511.             return None
  512.         self.db.delete_group(group)
  513.         print 
  514.  
  515.     do_delgrp = do_rmgrp
  516.     
  517.     def do_about(self, args):
  518.         '''About fab.'''
  519.         utils.log_title(__title__, __version__)
  520.  
  521.     
  522.     def do_import(self, args):
  523.         '''
  524.         Import LDIF
  525.         import <filename> [type]
  526.         [type] = vcf|ldif|auto
  527.         '''
  528.         args = args.strip().split()
  529.         if not args:
  530.             log.error('You must specify a filename to import from.')
  531.             return None
  532.         filename = args[0]
  533.         if len(args) > 1:
  534.             typ = args[1].lower()
  535.         else:
  536.             typ = 'auto'
  537.         if typ not in ('auto', 'ldif', 'vcf', 'vcard'):
  538.             log.error('Invalid type: %s' % typ)
  539.             return None
  540.         if not os.path.exists(filename):
  541.             log.error('File %s not found.' % filename)
  542.             return None
  543.         if typ == 'ldif':
  544.             print 'Importing from LDIF file %s...' % filename
  545.             (ok, error_str) = self.db.import_ldif(filename)
  546.         elif typ in ('vcard', 'vcf'):
  547.             print 'Importing from VCF file %s...' % filename
  548.             (ok, error_str) = self.db.import_vcard(filename)
  549.         
  550.         if not ok:
  551.             log.error(error_str)
  552.         else:
  553.             self.do_list('')
  554.         print 
  555.  
  556.  
  557. mod = module.Module(__mod__, __title__, __version__, __doc__, None, (GUI_MODE, INTERACTIVE_MODE), (UI_TOOLKIT_QT3, UI_TOOLKIT_QT4))
  558. mod.setUsage(module.USAGE_FLAG_NONE)
  559. (opts, device_uri, printer_name, mode, ui_toolkit, loc) = mod.parseStdOpts(handle_device_printer = False)
  560. if ui_toolkit == 'qt3':
  561.     if not utils.canEnterGUIMode():
  562.         log.error('%s GUI mode requires GUI support (try running with --qt4). Entering interactive mode.' % __mod__)
  563.         mode = INTERACTIVE_MODE
  564.     
  565. elif not utils.canEnterGUIMode4():
  566.     log.error('%s GUI mode requires GUI support (try running with --qt3). Entering interactive mode.' % __mod__)
  567.     mode = INTERACTIVE_MODE
  568.  
  569. if mode == GUI_MODE:
  570.     if ui_toolkit == 'qt3':
  571.         log.set_module('hp-fab(qt3)')
  572.         
  573.         try:
  574.             from qt import *
  575.             from ui.faxaddrbookform import FaxAddrBookForm
  576.         except ImportError:
  577.             log.error('Unable to load Qt3 support. Is it installed?')
  578.             sys.exit(1)
  579.  
  580.         app = None
  581.         addrbook = None
  582.         app = QApplication(sys.argv)
  583.         if loc is None:
  584.             loc = user_conf.get('ui', 'loc', 'system')
  585.             if loc.lower() == 'system':
  586.                 loc = str(QTextCodec.locale())
  587.                 log.debug('Using system locale: %s' % loc)
  588.             
  589.         
  590.         if loc.lower() != 'c':
  591.             e = 'utf8'
  592.             
  593.             try:
  594.                 (l, x) = loc.split('.')
  595.                 loc = '.'.join([
  596.                     l,
  597.                     e])
  598.             except ValueError:
  599.                 l = loc
  600.                 loc = '.'.join([
  601.                     loc,
  602.                     e])
  603.  
  604.             log.debug('Trying to load .qm file for %s locale.' % loc)
  605.             trans = QTranslator(None)
  606.             qm_file = 'hplip_%s.qm' % l
  607.             log.debug('Name of .qm file: %s' % qm_file)
  608.             loaded = trans.load(qm_file, prop.localization_dir)
  609.             if loaded:
  610.                 app.installTranslator(trans)
  611.             else:
  612.                 loc = 'c'
  613.         
  614.         if loc == 'c':
  615.             log.debug("Using default 'C' locale")
  616.         else:
  617.             log.debug('Using locale: %s' % loc)
  618.             QLocale.setDefault(QLocale(loc))
  619.             prop.locale = loc
  620.             
  621.             try:
  622.                 locale.setlocale(locale.LC_ALL, locale.normalize(loc))
  623.             except locale.Error:
  624.                 pass
  625.  
  626.         addrbook = FaxAddrBookForm()
  627.         addrbook.show()
  628.         app.setMainWidget(addrbook)
  629.         
  630.         try:
  631.             log.debug('Starting GUI loop...')
  632.             app.exec_loop()
  633.         except KeyboardInterrupt:
  634.             pass
  635.  
  636.         sys.exit(0)
  637.     else:
  638.         
  639.         try:
  640.             from PyQt4.QtGui import QApplication
  641.             from ui4.fabwindow import FABWindow
  642.         except ImportError:
  643.             log.error('Unable to load Qt4 support. Is it installed?')
  644.             sys.exit(1)
  645.  
  646.         log.set_module('hp-fab(qt4)')
  647.         app = QApplication(sys.argv)
  648.         fab = FABWindow(None)
  649.         fab.show()
  650.         
  651.         try:
  652.             log.debug('Starting GUI loop...')
  653.             app.exec_()
  654.         except KeyboardInterrupt:
  655.             sys.exit(0)
  656.  
  657. else:
  658.     
  659.     try:
  660.         from fax import fax
  661.     except ImportError:
  662.         log.error('Fax address book disabled - Python 2.3+ required.')
  663.         sys.exit(1)
  664.  
  665.     console = Console()
  666.     
  667.     try:
  668.         console.cmdloop()
  669.     except KeyboardInterrupt:
  670.         log.error('User exit.')
  671.  
  672.     log.info('')
  673.     log.info('Done.')
  674.